package net.cloudcodex.client.storyboard.command.presenter;

import java.util.Date;
import java.util.Map;

import net.cloudcodex.client.storyboard.command.view.CommandView;
import net.cloudcodex.client.storyboard.command.view.CommandView.CommandTab;
import net.cloudcodex.client.storyboard.player.PlayerBoardEventBus;
import net.cloudcodex.client.util.rpc.SuccessCallback;
import net.cloudcodex.shared.dto.Result;
import net.cloudcodex.shared.rpc.CampaignRPCAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.inject.Inject;
import com.mvp4g.client.presenter.BasePresenter;

/**
 * 
 * 
 * @author ADaroussin
 */
public abstract class CommandPresenter<V extends CommandView, E extends PlayerBoardEventBus> extends BasePresenter<V, E> {
	protected final CampaignRPCAsync service;

	private long campaignId;
	private long characterId;
	private long sceneId;
	private Date sceneTimestamp;

	@Inject
	public CommandPresenter(CampaignRPCAsync service) {
		this.service = service;
	}

	@Override
	public void bind() {
		super.bind();

		final CommandTab actionTab = this.view.getActionTab();
		actionTab.getButton().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				service.playerPostAction(campaignId, characterId, sceneId, sceneTimestamp, view.getMessageActionType(),
						actionTab.getMessage(), new SuccessCallback<Result<Boolean>>() {
							@Override
							public void onSuccess(Result<Boolean> result) {
								actionTab.emptyMessage();
								eventBus.checkScene(campaignId, characterId);
								GWT.log("result:" + result.getData());
							}
						});
				eventBus.checkScene(campaignId, characterId);
			}
		});
		final CommandTab offTab = this.view.getOffTab();
		offTab.getButton().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				service.playerPostOFF(campaignId, characterId, sceneId, sceneTimestamp, offTab.getMessage(), new SuccessCallback<Result<Boolean>>() {
					@Override
					public void onSuccess(Result<Boolean> result) {
						offTab.emptyMessage();
						eventBus.checkScene(campaignId, characterId);
						GWT.log("result:" + result.getData());
					}
				});
				eventBus.checkScene(campaignId, characterId);
			}
		});
		final CommandTab diceTab = this.view.getDiceTab();
		diceTab.getButton().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				final Map<Integer, Integer> dices = view.getDices();
				service.playerRollDices(campaignId, characterId, sceneId, sceneTimestamp, dices,
						diceTab.getMessage(), new SuccessCallback<Result<Boolean>>() {
							@Override
							public void onSuccess(Result<Boolean> result) {
								diceTab.emptyMessage();
								view.emptyDice();
								eventBus.checkScene(campaignId, characterId);
								GWT.log("result:" + result.getData());
							}
						});
				eventBus.checkScene(campaignId, characterId);
			}
		});
		this.view.getMoreDiceButton().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				view.newDiceLine();
			}
		});
	}

	public void setCampaignId(long campaignId) {
		this.campaignId = campaignId;
	}

	public void setCharacterId(long characterId) {
		this.characterId = characterId;
	}

	public void setSceneId(long sceneId) {
		this.sceneId = sceneId;
	}

	public void setSceneTimestamp(Date sceneTimestamp) {
		this.sceneTimestamp = sceneTimestamp;
	}
}
